package com.gateway.client.core.register;

import com.gateway.client.core.shutdown.GatewayClientShutdownHook;
import com.gateway.register.client.api.GatewayClientRegisterRepository;
import com.gateway.register.client.nacos.NacosClientRegisterRepository;
import com.gateway.register.common.config.GatewayRegisterCenterConfig;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/1/29 23:20
 **/
public class GatewayClientRegisterRepositoryFactory {
    private final static Map<String, GatewayClientRegisterRepository> REPOSITORY_MAP = new ConcurrentHashMap<>();

    /**
     * New instance gateway client register repository.
     * @param gatewayRegisterCenterConfig
     * @return
     */
    public static GatewayClientRegisterRepository newInstance(final GatewayRegisterCenterConfig gatewayRegisterCenterConfig) {
        if (!REPOSITORY_MAP.containsKey(gatewayRegisterCenterConfig.getRegisterType())) {
            NacosClientRegisterRepository repository = new NacosClientRegisterRepository();
            repository.init(gatewayRegisterCenterConfig);
            GatewayClientShutdownHook.set(repository, gatewayRegisterCenterConfig.getProps());
            REPOSITORY_MAP.put(gatewayRegisterCenterConfig.getRegisterType(), repository);
        }
        return REPOSITORY_MAP.get(gatewayRegisterCenterConfig.getRegisterType());
    }
}
